package com.dhcc.DRGsHosp.infra.util;

import java.io.BufferedReader;
import java.io.Reader;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Clob;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * 
 * @title		:StringUtils.java
 * @description	:字符串工具类 ，继承了  {@link org.springframework.util.StringUtils}
 * 				在方法上扩展了 {@link org.apache.commons.lang.StringUtils} 上几个常用的方法
 * 				尽量使用本工具类中提供的方法。
 * @author		:liqingyang
 * @date		:2014年3月17日
 * @version     :1.0
 * @copyright   :DHCC 2013-2014
 */
public class MonitorResultUtil extends org.springframework.util.StringUtils
{
	private final static Logger logger = LoggerFactory.getLogger(MonitorResultUtil.class);

	/**
	 * 获取上一个周期
	 */
	public static String getLastMrDate(String mrDate, String mrType){
		if(mrDate.length() == 5){
			int year = Integer.parseInt(mrDate.substring(0, 4))-1;
			return year+"Y";
		}
		String regex="[^\\d\r\n]*?(\\d+)[^\r\n]*?";
		Pattern p=Pattern.compile(regex);
	    Matcher m=p.matcher(mrDate);
	    m.find();
		int year = Integer.parseInt(m.group(1));
		m.find();
		int month = Integer.parseInt(m.group(1));
		if("M".equals(mrType)){
			if(month==1){
				month=12;
				year=year-1;
			}else{
				month=month-1;
			}
			mrDate = year+"Y"+month+"M";
		}else if("Q".equals(mrType)){
			if(month==1){
				month=4;
				year=year-1;
			}else{
				month=month-1;
			}
			mrDate = year+"Y"+month+"Q";
		}else if("HF".equals(mrType)){
			if(month==1){
				month=2;
				year=year-1;
			}else{
				month=month-1;
			}
			mrDate = year+"Y"+month+"HF";
		}else if("Y".equals(mrType)){
			year=year-1;
			mrDate = year+"Y";
		}else if("MC".equals(mrType)){
			if(month==1){
				month=12;
				year=year-1;
			}else{
				month=month-1;
			}
			mrDate = year+"Y"+month+"MC";
//			year=year-1;
//			mrDate = year+"Y";
		}else{
			mrDate = "";
		}
		return mrDate;
	}
	
	/**
	 * 获取同周期
	 */
	public static String getLastYearMrDate(String mrDate){
		String regex="[^\\d\r\n]*?(\\d+)[^\r\n]*?";
		Pattern p=Pattern.compile(regex);
	    Matcher m=p.matcher(mrDate);
	    m.find();
		int year = Integer.parseInt(m.group(1));
		year--;
//		if(mrDate.indexOf("MC")>0){
////			return year + "Y";
//			return year + "Y" + "MC";
//		}
		return mrDate.replaceAll("\\d{4}", ""+year);
	}
	
	/**
	 * @methodname    :getPreviousYearByDate
	 * @description   : 根据日期获取上一年同期
	 * @author        :lileizheng
	 * @date          :2016年12月2日
	 */
	public static String getPreviousYearByDate(String date){
		String mrDate = addMonth(date, -12);
		return mrDate;
	}
	
	/**
	 * @methodname    :getPreviousYearByDate
	 * @description   : 根据日期获取上一个月上期
	 * @author        :lileizheng
	 * @date          :2016年12月2日
	 */
	public static String getPreviousMonthByDate(String date){
		String mrDate = addMonth(date, -1);
		return mrDate;
	}
	
	/**
	 * @methodname    :addMonth
	 * @description   :日期加减月
	 * @param         :
	 * @author        :lileizheng
	 * @return        :
	 * @date          :2016年12月2日
	 */
	public static String addMonth(String date, int m){
		try {
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			if(date.length()<8){
				format = new SimpleDateFormat("yyyy-MM");
			}
			Calendar c = Calendar.getInstance(); 
			Date d = format.parse(date);  
			c.setTime(d);
			try {
				if(isLastDayOfMonth(date)){
					//月底,直接过去上m个月最后一天
					c.add(Calendar.MONTH, m+1);
					c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), 0);
				} else{
					c.add(Calendar.MONTH, m);
				}
			} catch (Exception e) {
				c.add(Calendar.MONTH, m);
			} 
			
			String now = format.format(c.getTime());
		    return now;
		} catch (ParseException e) {
			e.printStackTrace();
		}
    	return date;
	}
	
	/**
	 * @methodname    :addDay
	 * @description   :获取当前时间的前一天时间,后一天,前几天,后几天…… 
	 * @param         :
	 * @author        :lileizheng
	 * @return        :
	 * @date          :2017年11月7日
	 */
	public static String addDay(String date, int d){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
		Calendar c = Calendar.getInstance(); 
		Date da = new Date();
		try {
			da = format.parse(date);
		} catch (ParseException e) {
			e.printStackTrace();
		}  
		c.setTime(da);
		//使用set方法直接进行设置
		int day = c.get(Calendar.DATE);
		c.set(Calendar.DATE, day+d);
		String beforeDay = format.format(c.getTime());
		return beforeDay;
	}
	
	//判定一个日期为月末最后一天
	public static boolean isLastDayOfMonth(String date) throws ParseException { 
		Date d = new SimpleDateFormat("yyyy-MM-dd").parse(date);
        Calendar calendar = Calendar.getInstance(); 
        calendar.setTime(d); 
        calendar.set(Calendar.DATE, (calendar.get(Calendar.DATE) + 1)); 
        if (calendar.get(Calendar.DAY_OF_MONTH) == 1) { 
            return true; 
        } 
        return false; 
    } 
	
	/**
	 * 根据批次获取周期起始日期
	 * @param mrLot
	 * @return 
	 */
	public static String getStartDateByLot(String mrLot, String mrType){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		String year = mrLot.substring(0, 4);
		if("MC".equals(mrType)){
			return year+"-01-01";
		}else if("Q".equals(mrType)){
			mrLot = mrLot.replace(mrType, "");
			String q = mrLot.substring(mrLot.indexOf("Y")+1);
			Calendar c = Calendar.getInstance(); 
			c.set(Integer.parseInt(year), 1, 1);
	        String now = null; 
	        try { 
	            if ("1".equals(q)) 
	                c.set(Calendar.MONTH, 0); 
	            else if ("2".equals(q)) 
	                c.set(Calendar.MONTH, 3); 
	            else if ("3".equals(q)) 
	                c.set(Calendar.MONTH, 6); 
	            else if ("4".equals(q)) 
	                c.set(Calendar.MONTH, 9); 
	            c.set(Calendar.DATE, 1); 
	            now = format.format(c.getTime());
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } 
	        return now; 
		}else if("M".equals(mrType)){
			mrLot = mrLot.replace(mrType, "");
			String month = mrLot.substring(mrLot.indexOf("Y")+1);
			Calendar c = Calendar.getInstance(); 
			c.set(Integer.parseInt(year), Integer.parseInt(month)-1, 1);
	        String now = null; 
	        try { 
	            c.add(Calendar.DATE, 0); 
	            now = format.format(c.getTime());
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } 
	        return now;
		}else if("Y".equals(mrType)){
			Calendar c = Calendar.getInstance(); 
			c.set(Integer.parseInt(year), 0, 1);
			c.set(Calendar.MONTH, 0); 
            c.set(Calendar.DATE, 1); 
            String now = null; 
            try {
				now = format.format(c.getTime());
			} catch (Exception e) {
			}
			return now;
		}
		return null;
	}
	
	/**
	 * 根据批次获取周期结束时间
	 * @param list
	 * @return
	 */
	public static String getEndDateByLot(String mrLot, String mrType){
		SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		String year = mrLot.substring(0, 4);
		if("MC".equals(mrType) || "M".equals(mrType)){
			mrLot = mrLot.replace(mrType, "");
			String month = mrLot.substring(mrLot.indexOf("Y")+1);
			if(!StringUtils.hasLength(month)){
				Calendar c = Calendar.getInstance(); 
				c.set(Integer.parseInt(year), 1, 1);
				String now = null;
				try { 
		            c.set(Calendar.MONTH, 11); 
		            c.set(Calendar.DATE, 31);
		            now = format.format(c.getTime());
		        } catch (Exception e) { 
		            e.printStackTrace(); 
		        } 
				return now;
			}
			Calendar c = Calendar.getInstance(); 
			c.set(Integer.parseInt(year), Integer.parseInt(month)-1, 1);
	        String now = null; 
	        try { 
	            c.set(Calendar.DATE, 1);
	            c.add(Calendar.MONTH, 1); 
	            c.add(Calendar.DATE, -1); 
	            now = format.format(c.getTime());
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } 
	        return now; 
		}else if("Q".equals(mrType)){
			mrLot = mrLot.replace(mrType, "");
			String q = mrLot.substring(mrLot.indexOf("Y")+1);
			Calendar c = Calendar.getInstance(); 
			c.set(Integer.parseInt(year), 1, 1);
	        String now = null; 
	        try { 
	            if ("1".equals(q)) 
	                c.set(Calendar.MONTH, 2); 
	            else if ("2".equals(q)) 
	                c.set(Calendar.MONTH, 5); 
	            else if ("3".equals(q)) 
	                c.set(Calendar.MONTH, 8); 
	            else if ("4".equals(q)) 
	                c.set(Calendar.MONTH, 11); 
	            c.set(Calendar.DATE, 1);
	            c.add(Calendar.MONTH, 1); 
	            c.add(Calendar.DATE, -1);
	            now = format.format(c.getTime());
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } 
	        return now; 
		}else if("Y".equals(mrType)){
			Calendar c = Calendar.getInstance(); 
			c.set(Integer.parseInt(year), 1, 1);
			String now = null;
			try { 
	            c.set(Calendar.MONTH, 11); 
	            c.set(Calendar.DATE, 31);
	            now = format.format(c.getTime());
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } 
			return now;
		}
		return null;
	}
	
	//null转字符串
	public static Object nullToStr(Object o){
		return o==null?"":o;
	}
	
	//Object转double
	public static Double objectToDouble(Object o){
		double value = 0d;
		if(o==null ||"".equals(o)) return value;
		String ostr = o.toString();
		ostr = ostr.replace(",", "");
		try{
			value = Double.valueOf(ostr);
		}catch(Exception e){
			value = 0d;
		}
		return value;
	}
	
	//Object转date
		public static Date objectToDate(Object o){
			SimpleDateFormat sDateFormat=new SimpleDateFormat("yyyy/MM/dd"); 
			Date value = null;
			if(o==null ||"".equals(o)) return value;
			String ostr = o.toString();
			 try {
				 value=sDateFormat.parse(ostr);
		        } catch(ParseException px) {
		            px.printStackTrace();
		        }
			return value;
		}
	
	//统计汉字个数，一个英文、数字、其他字符算0.5个
	public static float countWords(String str) {
        if (str == null || str.length() <= 0) {
            return 0;
        }
        float len = 0;
        char c;
        for (int i = str.length() - 1; i >= 0; i--) {
            c = str.charAt(i);
            if ((c >= '0' && c <= '9') || (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z')) {
                // 字母, 数字
                len+=0.5;
            } else {
                if (Character.isLetter(c)) { // 中文
                    len++;
                } else { // 符号或控制字符
                    len+=0.5;
                }
            }
        }
        return len;
    }
	
	public static String limitStr(String str, int limit){
		float slength = countWords(str);
	     if(slength>5){
	    	 str =str.substring(0, 5);
	    	 str =str+"...";
	     }
	     return str;
	}
	
	//获取double的位数
	private static int getNumFromDouble(double d){
		int i = (int)d;
		String s = String.valueOf(i);
		int i1 = s.length();
		return i1;
	}
	
	//获取大于double位数的十进制数
	public static double getDoubleX(double d){
		if(d>=0 && d<=1){
			return 1;
		}
		if(d>=80 && d<=100){
			return 100;
		}
		double d1 = getFirstNum(d)+1;
		int i = getNumFromDouble(d);
		for(int k=0; k<i-1; k++){
			d1 *= 10;
		}
		return d1;
	}
	
	//获取最高位数
	private static int getFirstNum(double a){
		int b = (int)a;
		int m = 1;  
		for (int i = 0; i < (int) Math.log10(a); i++) {  
		    m *= 10;  
		}  
		return (int) (a / m);
	}
	
	//获取大于double位数的十进制数
	public static double getDoubleM(double d){
		if(d>=0 && d<=1){
			return 1;
		}
		if(d>=80 && d<=100){
			return 100;
		}
		double d1 = getFirstNum(d);
		int i = getNumFromDouble(d);
		for(int k=0; k<i-1; k++){
			d1 *= 10;
		}
		return d1;
	}
	
	/**
	 * @methodname    :ConvertObjToMap
	 * @description   : 实体类转Map
	 * @param         :
	 * @author        :lileizheng
	 * @return        :
	 * @date          :2015年5月21日
	 */
	public static Map ConvertObjToMap(Object obj){
        Map<String,Object> reMap = new HashMap<String,Object>();
        if (obj == null) 
         return null;
        Field[] fields = obj.getClass().getDeclaredFields();
        try {
         for(int i=0;i<fields.length;i++){
          try {
           Field f = obj.getClass().getDeclaredField(fields[i].getName());
           f.setAccessible(true);
                 Object o = f.get(obj);
                 reMap.put(fields[i].getName(), o);
          } catch (NoSuchFieldException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          } catch (IllegalArgumentException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          } catch (IllegalAccessException e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
          }
         }
        } catch (SecurityException e) {
         // TODO Auto-generated catch block
         e.printStackTrace();
        } 
        return reMap;
       }
	
	/**  
     * 两个字符类型的小数进行相加为a+b;  
     *   
     * @param a  
     * @param b  
     * @return  
     */   
    public static String addBigDecimal(String a, String b) {    
        double a1 = Double.parseDouble(a);    
        double b1 = Double.parseDouble(b);    
        BigDecimal a2 = BigDecimal.valueOf(a1);    
        BigDecimal b2 = BigDecimal.valueOf(b1);    
        BigDecimal c2 = a2.add(b2);    
        String c1 = c2 + "";    
        return c1;    
    }   
    /**  
     * 两个字符类型的小数进行相减为a-b;  
     *   
     * @param a  
     * @param b  
     * @return  
     */   
    public static String reduceBigDecimal(String a, String b) {    
        double a1 = Double.parseDouble(a);    
        double b1 = Double.parseDouble(b);    
        BigDecimal a2 = BigDecimal.valueOf(a1);    
        BigDecimal b2 = BigDecimal.valueOf(b1);    
        BigDecimal c2 = a2.subtract(b2);    
        String c1 = c2 + "";    
        return c1;    
    }   
    /**  
     * 两个字符类型的数相乘 a*b=c；  
     *   
     * @param a  
     * @param b  
     * @return  
     */   
    public static String multipliedString(String a, String b) {    
        double a1 = Double.parseDouble(a);    
        double b1 = Double.parseDouble(b);    
        BigDecimal a2 = BigDecimal.valueOf(a1);    
        BigDecimal b2 = BigDecimal.valueOf(b1);    
        BigDecimal c2 = a2.multiply(b2);    
        String c1 = c2 + "";    
        return c1;    
    }   
    /**  
     * 两个字符类型的数相除 a/b=c；  
     *   
     * @param a  
     * @param b  
     * @return  
     */   
    public static String divideString(String a, String b, int scale) {    
        double a1 = Double.parseDouble(a);    
        double b1 = Double.parseDouble(b);    
        BigDecimal a2 = BigDecimal.valueOf(a1);    
        BigDecimal b2 = BigDecimal.valueOf(b1);    
        BigDecimal c2 = a2.divide(b2,scale,BigDecimal.ROUND_HALF_EVEN);    
        String c1 = c2.doubleValue() + "";    
        return c1;    
    }   
   
    public static String yunsuanjibie(String s, int scale) { 
        String r = ""; 
        int p = 0; 
        for (int i = 0; i < s.length(); i++) { 
            if (s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' 
                    || s.charAt(i) == '/') { 
                p++; 
            } 
        } 
        String k[] = new String[2 * p + 1]; 
        int k1 = 0; 
        int first = 0; 
        for (int i = 0; i < s.length(); i++) { 
            if (s.charAt(i) == '+' || s.charAt(i) == '-' || s.charAt(i) == '*' 
                    || s.charAt(i) == '/') { 
                k[k1] = s.substring(first, i); 
                k1++; 
                k[k1] = "" + s.charAt(i); 
                k1++; 
                first = i + 1; 
            } 
        } 
        k[k1] = s.substring(first, s.length()); 
        int kp = p; 
        while (kp > 0) { 
            for (int i = 0; i < k.length; i++) { 
                if (k[i].equals("*") || k[i].equals("/")) { 
                    int l; 
                    for (l = i - 1; l > -1; l--) { 
                        if (!(k[l].equals("p"))) 
                            break; 
                    } 
                    int q; 
                    for (q = i + 1; q < k.length; q++) { 
                        if (!(k[l].equals("p"))) 
                            break; 
                    } 
                    if (k[i].equals("*")) { 
                        k[i] = ""+ multipliedString(k[l],k[q]); 
                        k[l] = "p"; 
                        k[q] = "p"; 
                        kp--; 
                    } else { 
                        k[i] = ""+divideString(k[l],k[q],scale); 
                        k[l] = "p"; 
                        k[q] = "p"; 
                        kp--; 
                    } 
                    break; 
                } 
            } 
            for (int i = 0; i < 2 * p + 1; i++) { 
                if (k[i].equals("+") || k[i].equals("-")) { 
                    int l; 
                    for (l = i - 1; l > -1; l--) { 
                        if (!(k[l].equals("p"))) 
                            break; 
                    } 
                    int q; 
                    for (q = i + 1; q < k.length; q++) { 
                        if (!(k[q].equals("p"))) 
                            break; 
                    } 
                    if (k[i].equals("+")) { 
                        k[i] = ""+addBigDecimal(k[l],k[q]); 
                        k[l] = "p"; 
                        k[q] = "p"; 
                        kp--; 
                    } else { 
                        k[i] = ""+reduceBigDecimal(k[l],k[q]); 
                        k[l] = "p"; 
                        k[q] = "p"; 
                        kp--; 
                    } 
                    break; 
                } 
            } 
            for (int i = 0; i < k.length; i++) { 
                if (!(k[i].equals("p"))) { 
                    r = k[i]; 
                    break; 
                } 
            } 
        } 
        return r; 
    } 
   
    public static String sizeyunsuan(String s, int scale) { 
        while (true) { 
            int first = 0; 
            int last = 0; 
            for (int i = 0; i < s.length(); i++) { 
                if (s.charAt(i) == '(') 
                    first = i; 
                if (s.charAt(i) == ')') { 
                    last = i; 
                    break; 
                } 
            } 
            if (last == 0) { 
            	BigDecimal bd = new BigDecimal(yunsuanjibie(s,scale));
            	//
                return yunsuanjibie(s,scale); 
            } else { 
                String s1 = s.substring(0, first); 
                String s2 = s.substring(first + 1, last); 
                String s3 = s.substring(last + 1, s.length()); 
                s = s1 + yunsuanjibie(s2,scale) + s3; 
            } 
        } 
    } 
    
    /*
     * 获取月份开始日期 参数格式2014-01 输出2014-01-01
     */
    public static String getStartDateMonth(String dateFrom){
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	Calendar c = Calendar.getInstance(); 
    	if(dateFrom != null && dateFrom.length()>=7){
    		String year = dateFrom.substring(0, 4);
    		String month = dateFrom.substring(5, 7);
			c.set(Integer.parseInt(year), Integer.parseInt(month)-1, 1);
			String now = null; 
	        try { 
	            c.add(Calendar.DATE, 0); 
	            now = format.format(c.getTime());
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } 
	        return now;
    	}
    	return null;
    }
    
    /*
     * 获取月份结束日期 参数格式2014-01 输出2014-01-31
     */
    public static String getEndDateMonth(String dateTo){
    	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
    	Calendar c = Calendar.getInstance(); 
    	if(dateTo != null && dateTo.length()>=7){
    		String year = dateTo.substring(0, 4);
    		String month = dateTo.substring(5, 7);
    		c.set(Integer.parseInt(year), Integer.parseInt(month)-1, 1);
	        String now = null; 
	        try { 
	            c.set(Calendar.DATE, 1);
	            c.add(Calendar.MONTH, 1); 
	            c.add(Calendar.DATE, -1); 
	            now = format.format(c.getTime());
	        } catch (Exception e) { 
	            e.printStackTrace(); 
	        } 
	        return now; 
    	}
    	return null;
    }
    
    //oracle.sql.Clob类型转换成String类型
	public static String ClobToString(Clob clob) throws Exception {
		String reString = "";
		Reader is = clob.getCharacterStream();// 得到流
		BufferedReader br = new BufferedReader(is);
		String s = br.readLine();
		StringBuffer sb = new StringBuffer();
		while (s != null) {// 执行循环将字符串全部取出付值给StringBuffer由StringBuffer转成STRING
			sb.append(s);
			sb.append(" \n ");
			s = br.readLine();
		}
		reString = sb.toString();
		return reString;
	}
	
	//Object对象转String
	public static String object2String(Object o){
		return o!=null?o.toString():"";
	}

	//Object对象转String
	public static String object2String(Object o,String def){
		return o!=null?o.toString().trim():def;
	}

	//比较值拼接,用于monitor多指标查询
		public static List<Map<String, Object>> mapListcc(List<Map<String, Object>> list1, List<Map<String, Object>> list2, String t, int startOrEnd){
	    	int i=0;
	    	int j=0;
	    	int iMax=list1.size();
	    	int jMax=list2.size();
	    	if(iMax==0 || jMax==0) return list1;
			for(i=0; i<iMax; i++){
	    		int k=j;
	    		Map<String, Object> map1 = list1.get(i);
	    		String ccode1 = map1.get("ccode")!=null?map1.get("ccode").toString():"";
	    		while(true){
	    			if(k==jMax) break;
	    			Map<String, Object> map2 = list2.get(k);
	        		String ccode2 = map2.get("ccode")!=null?map2.get("ccode").toString():"";
	    			if(ccode2.equals(ccode1)){
	    				 for(String dataKey : map2.keySet())   { 
	    					 if(startOrEnd==1){
	    						 map1.put(dataKey+t, map2.get(dataKey));
	    					 }else{
	    						 map1.put(t+dataKey, map2.get(dataKey));
	    					 }
	    				 } 
	    				 j++;
	    				 break;
	    			}
	    			k++;
	    		}
	    	}
	    	return list1;
	    }
	
	// 根据Unicode编码完美的判断中文汉字和符号
	private static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}

	// 完整的判断中文汉字和符号
	public static boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}
	
	//判定字符串中字符个数
	public static int countInnerStr(final String str, final String patternStr) {
		int count = 0;
		final Pattern r = Pattern.compile(patternStr);
		final Matcher m = r.matcher(str);
		while (m.find()) {
			count++;
		}
		return count;
	}
	
	/**
	 *  四舍五入
	 *  round.  
	 *  @Description：
	 *  @Author ChenHanlin
	 *  @param dv 数字
	 *  @param scale 大于0
	 *  @return 
	 *  @Date:2018年5月14日下午3:07:59
	 */
    public static Double round(String dv,int scale){
    	if(null == dv){
    		return null ;
    	} else {
            return Arith.div(Double.parseDouble(dv.toString()), 1,scale);
    	}
    }

	/**
	 * 数学运算
	 * @param operator
	 * @param a
	 * @param b
	 * @return
	 */
	public static double calculator(String operator,Object a,Object b) {
		Double ad = MonitorResultUtil.objectToDouble(a) ;
		Double bd = MonitorResultUtil.objectToDouble(b) ;
		Double r = null ;
		switch(operator){
			case "+": r = Arith.add(ad, bd) ;
				break;
			case "-": r = Arith.sub(ad, bd) ;
				break;
			case "*": r = Arith.mul(ad, bd) ;
				break;
			case "/": r = Arith.div(ad, bd) ;
				break;
			default: r = 0.0 ;
		}
		return r;
	};

	/**
	 * 数学运算,限制保留位数
	 * @param operator
	 * @param a
	 * @param b
	 * @param scale
	 * @return
	 */
	public static double calculator(String operator,Object a,Object b,int scale) {
		Double ad = MonitorResultUtil.objectToDouble(a) ;
		Double bd = MonitorResultUtil.objectToDouble(b) ;
		Double r = null ;
		switch(operator){
			case "+": r = Arith.add(ad, bd) ;
				break;
			case "-": r = Arith.sub(ad, bd) ;
				break;
			case "*": r = Arith.mul(ad, bd) ;
				break;
			case "/": r = Arith.div(ad, bd) ;
				break;
			default: r = 0.0 ;
		}
		return Arith.round(r, scale) ;
	};

	/**
	 * 数学运算,限制保留位数
	 * @param operator
	 * @param a
	 * @param b
	 * @param scale
	 * @return
	 */
	public static String calculator(String operator,Object a,Object b,String type) {
		Double ad = MonitorResultUtil.objectToDouble(a) ;
		Double bd = MonitorResultUtil.objectToDouble(b) ;
		Double r = null ;
		switch(operator){
			case "+": r = Arith.add(ad, bd) ;
				break;
			case "-": r = Arith.sub(ad, bd) ;
				break;
			case "*": r = Arith.mul(ad, bd) ;
				break;
			case "/": r = Arith.div(ad, bd) ;
				break;
			default: r = 0.0 ;
		}
		return Arith.dataFormat((Arith.round(r,2))+"", type) ;
	};

	/**
	 * 对象格式化
	 * @param o
	 * @param type
	 * @return
	 */
	public static String dataFormat(Object o,String type){
		String a = object2String(o) ;
		DecimalFormat df = new DecimalFormat("0.00");//1
		DecimalFormat money = new DecimalFormat("#,###");//2
		DecimalFormat moneydb = new DecimalFormat("#,##0.00");//3
		String mya=(a.trim()==null ||a.trim()==""||a.trim()=="undefined" ||!Arith.isNumeric(a.trim()))?"0" :a.trim();
		String m="";
		if("1".equals(type)){
			m=df.format(Double.valueOf(mya).doubleValue()) ;
		}else if ("2".equals(type)) {
			m=money.format(Double.valueOf(mya).intValue()) ;
		}else if ("3".equals(type)) {
			m=moneydb.format(Double.valueOf(mya).doubleValue()) ;
		}
		return m;
	}


	/**
	 * 
	 * @methodname    :main
	 * @description   : 
	 * @param         :
	 * @author        :liqingyang
	 * @return        :
	 * @date          :2014年3月17日
	 */
	public static void main(String[] args)
	{
		System.err.println(round("3.155",-1));
	}
}